from vpython import *

g = 9.8
L = 0.15
m = 0.25
T = 5

top = box(pos=vector(0, L/2, 0), size = vector(L, L/10, L/5))
pivot = sphere(pos = top.pos - vector(0, top.size.y/2, 0), radius = L/50)

theta1 = acos(m*g/T)
pig = sphere(pos = pivot.pos + L*vector(sin(theta1), - cos(theta1), 0), radius = L/20, color = color.blue, make_trail = True, trail_type = "points", retain = 50)
string = cylinder(pos = pivot.pos, axis = pig.pos - pivot.pos, radius = L/100)

theta2 = 0
R = L*sin(theta1)
t = 0
dt = 0.01
w = sqrt(T/(m*L))
circlecenter = pivot.pos - vector(0, L*cos(theta1), 0)

while t < 5:
    rate(100)
    pig.pos = circlecenter + R*vector(cos(theta2), 0, sin(theta2))
    theta2 = w*t
    string.axis = pig.pos - pivot.pos
    t = t + dt


